package cn.hx.mall.sell.controller;

import cn.hx.mall.sell.entity.SmsFlashPromotionProductRelation;
import cn.hx.mall.sell.entity.SmsFlashPromotionSession;
import cn.hx.mall.sell.service.SmsFlashPromotionProductRelationService;
import cn.hx.mall.sell.service.SmsFlashPromotionSessionService;
import cn.hx.mall.goods.entity.Product;
import cn.hx.mall.goods.service.ProductService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品限时购与商品关系表(SmsFlashPromotionProductRelation)表控制层
 *
 * @author makejava
 * @since 2022-11-02 16:10:30
 */
@Controller
@RequestMapping("sell/flashProductRelation")
public class SmsFlashPromotionProductRelationController {
    /**
     * 服务对象
     */
    @Resource
    private SmsFlashPromotionProductRelationService flashProductRelationService;

    @Resource
    private SmsFlashPromotionSessionService flashSessionService;

    @Resource
    private ProductService productService;

    /**
     * 秒杀场次列表
     *
     * @param flashPromotionId  所属秒杀活动id
     * @param map   秒杀场次数据
     * @return  秒杀场次列表页面
     */
    @GetMapping("/list")
    public String list(Long flashPromotionId, ModelMap map) {
        List<SmsFlashPromotionSession> flashSessions = flashSessionService.queryByPromotionId(flashPromotionId);
        map.addAttribute("flashSessions", flashSessions);
        map.addAttribute("flashPromotionId", flashPromotionId);
        return "sell/flashProductRelation";
    }

    /**
     * 秒杀场次中的商品列表
     *
     * @param map   页面中该展示的商品列表数据
     * @param flashProductRelation  秒杀活动及场次
     * @param page  当前页
     * @param size  每页条数
     * @return  秒杀商品列表页面
     */
    @GetMapping("/products")
    public String products(ModelMap map, SmsFlashPromotionProductRelation flashProductRelation,
                           @RequestParam(value = "page", defaultValue = "1") Integer page,
                           @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<SmsFlashPromotionProductRelation> flashProductRelations = PageHelper.startPage(page, size);
        flashProductRelationService.queryAll(flashProductRelation);
        PageInfo<SmsFlashPromotionProductRelation> pageInfo = new PageInfo<>(flashProductRelations);
        System.out.println("pageInfo===>" + pageInfo);
        map.addAttribute("pageInfo", pageInfo);
        return "sell/flashProductList";
    }

    /**
     * 添加秒杀商品
     *
     * @param flashPromotionId      商品添加到的秒杀活动
     * @param flashPromotionSessionId   商品添加到的秒杀场次
     * @param ids       添加的商品id集合
     * @param page      当前所在页
     * @param size      当前每页大小
     * @return     重定向到秒杀场次中的商品列表
     */
    @PostMapping("/addProducts")
    public String addProducts(Long flashPromotionId, Long flashPromotionSessionId,
                              @RequestParam("id") List<Long> ids,
                              @RequestParam(value = "page", defaultValue = "1") Integer page,
                              @RequestParam(value = "size", defaultValue = "10") Integer size) {
        System.out.println("flashPromotionId==>" + flashPromotionId);
        System.out.println("flashPromotionSessionId==>" + flashPromotionSessionId);
        System.out.println("要添加的商品id：" + ids);
        System.out.println("当前所在页：" + page + "当前每页条数：" + size);
        for (Long id : ids) {
            SmsFlashPromotionProductRelation flashProductRelation = new SmsFlashPromotionProductRelation();
            flashProductRelation.setFlashPromotionId(flashPromotionId);
            flashProductRelation.setFlashPromotionSessionId(flashPromotionSessionId);
            flashProductRelation.setProductId(id);
            flashProductRelationService.insert(flashProductRelation);
        }
        return "redirect:/sell/flashProductRelation/products?flashPromotionId=" + flashPromotionId
                + "&flashPromotionSessionId=" + flashPromotionSessionId + "&page=" + page + "&size=" + size;
    }

    /**
     * 根据关系id查询秒杀活动与秒杀商品信息
     *
     * @param id  关系表的id
     * @return    秒杀活动中信息及秒杀商品信息
     */
    @GetMapping("/queryById/{id}")
    @ResponseBody
    public Map<String, Object> queryById(@PathVariable Long id) {
        SmsFlashPromotionProductRelation flashProductRelation = flashProductRelationService.queryById(id);
        Map<String, Object> resMap = new HashMap<>(1);
        resMap.put("data", flashProductRelation);
        System.out.println("data==>" + resMap.get("data"));
        return resMap;
    }

    /**
     * 修改秒杀商品的信息
     *
     * @param flashProductRelation  提交的修改信息
     * @param page  当前页
     * @param size  每页条数
     * @return  重定向到秒杀场次中的商品列表
     */
    @PostMapping("/update")
    public String update(SmsFlashPromotionProductRelation flashProductRelation,
                         @RequestParam(value = "page",defaultValue = "1") Integer page,
                         @RequestParam(value = "size", defaultValue = "10") Integer size) {
        System.out.println("提交的修改数据==>" + flashProductRelation);
        Long flashPromotionId = flashProductRelation.getFlashPromotionId();
        Long flashPromotionSessionId = flashProductRelation.getFlashPromotionSessionId();
        flashProductRelationService.update(flashProductRelation);
        return "redirect:/sell/flashProductRelation/products?flashPromotionId=" + flashPromotionId
                + "&flashPromotionSessionId=" + flashPromotionSessionId + "&page=" + page + "&size=" + size;
    }

    /**
     * 查询所有商品信息，用于往秒杀场次中添加商品
     *
     * @param product   查询时的筛选条件（商品名）
     * @return          商品集合
     */
    @GetMapping("/queryProducts")
    @ResponseBody
    public Map<String, Object> queryAllProduct(Product product) {
        List<Product> products = productService.queryAll(product);
        Map<String, Object> resMap = new HashMap<>(1);
        resMap.put("data", products);
        return resMap;
    }

    /**
     * 根据id删除秒杀商品
     *
     * @param id  秒杀商品的id
     * @return  重定向到秒杀商品列表页面
     */
    @GetMapping("/delete/{id}")
    public String deleteById(@PathVariable Long id, Long flashPromotionId, Long flashPromotionSessionId,
                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        flashProductRelationService.deleteById(id);
        return "redirect:/sell/flashProductRelation/products?flashPromotionId=" + flashPromotionId
                + "&flashPromotionSessionId=" + flashPromotionSessionId + "&page=1&size=" + size;
    }

}